﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Boting.WPFCode.y23Window.ViewModels
{
    public partial class MainWindowViewModel: ObservableObject
    {
        #region private property
        /// <summary>
        /// the window this view model controls
        /// </summary>
        private Window _window;

        [ObservableProperty]
        public string title = "hello";
        #endregion

        #region Command

        [RelayCommand]
        private void Minimize()
        {
            _window.WindowState = WindowState.Minimized;
        }
        [RelayCommand]
        private void Maximize()
        {
            _window.WindowState ^= WindowState.Maximized;
        }
        [RelayCommand]
        private void Close()
        {
            //关闭，
            _window.Close();

        }

        [RelayCommand]
        private void Menu()
        {
            //系统菜单
            SystemCommands.ShowSystemMenu(_window, getMousePosition());
        }
        #endregion

        public MainWindowViewModel(Window window)
        {
            _window = window;
        }

        #region Method
        /// <summary>
        /// 获取光标位置
        /// </summary>
        /// <returns></returns>
        private Point getMousePosition()
        {
            var position = Mouse.GetPosition(_window);
            if (_window.WindowState == WindowState.Maximized)
            {//处理最大窗体时获取位置异常
                return new Point(position.X, position.Y);
            }
            return new Point(_window.Left + position.X, _window.Top + position.Y);
        }
        #endregion
    }
}
